<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class InitCodes extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('codes', function (Blueprint $table) {
            $table->comment = '统一兑换码表';

            $table->increments('id');
            $table->uuid('uuid')->index() ;

            $table->string('title')->nullable()->comment('名称（仅后台显示）');
            $table->string('code', 20)->index()->comment('兑换码');

            $table->integer('stock')->default(1)->comment('库存数量'); 
            $table->integer('sales')->default(0)->comment('已兑换数量'); 

            $table->string('code_type', 10)->index()->comment('兑换码类型, coupon为优惠卡券 card为道具卡  score为积分  redpack为红包'); 

            $table->integer('coupon_id')->index()->nullable()->comment('优惠券id');
            $table->integer('score')->index()->nullable()->comment('积分数量');
            $table->integer('redpack')->index()->nullable()->comment('红包数量');

            $table->string('card_type', 20)->index()->nullable()->comment('道具卡type');

            $table->integer('user_group_id')->index()->nullable()->comment('限制可用人群');

            $table->tinyInteger('status')->default(1)->index()->comment('0为未上架 1为已上架 '); // 0为未上架 1为已上架 

            $table->softDeletes();
            $table->timestamps();

        });

        Schema::create('code_records', function (Blueprint $table) {
            $table->comment = '兑换记录表';

            $table->increments('id');
            $table->uuid('uuid')->index() ;

            $table->integer('code_id')->index()->comment('兑换码id'); 
            $table->integer('user_id')->index()->comment('用户id'); 
            $table->integer('order_id')->index()->nullable()->comment('订单id'); 

            $table->string('client_type', 10)->index()->nullable()->comment('平台类型'); 
            $table->string('os_type', 10)->index()->nullable()->comment('系统类型'); 

            $table->softDeletes();
            $table->timestamps();

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::disableForeignKeyConstraints();
        Schema::dropIfExists('codes');
        Schema::dropIfExists('code_records');
    }
}
